- 
                Notifications
    You must be signed in to change notification settings 
- Fork 8.2k
lib: os: mpsc_pbuf: fix potential semaphore wait forever #97931
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: main
Are you sure you want to change the base?
Conversation
| Hello @fei315412-cmyk, and thank you very much for your first pull request to the Zephyr project! | 
| @nordic-krch @dcpleung this bug was found in production. please provide any suggestion , thanks . | 
| I am not familiar with mpsc_pbuf. | 
| @fei315412-cmyk but after calling  | 
| 
 can you tell me ? how can compile test case about mpsc.c and how to run ? give me example and i write test case and test @nordic-krch | 
| for example  | 
| @nordic-krch block backtraces as fallows: | 
ba8e29e    to
    9e95451      
    Compare
  
    | @nordic-krch @nashif @dcpleung   please provide any suggestion again , thanks . | 
One thread calls mpsc_pbuf_alloc to produce data, which invokes add_skip_item and steps into k_sem_take. Another thread calls mpsc_pbuf_claim to consume data. In this condition, mpsc_pbuf_claim has only small remaining space and needs to call rd_idx_inc to reserve space, but there is still no data available. The consumer should call k_sem_give to wake mpsc_pbuf_alloc again, so the producer can allocate space and continue producing data. Without this wake-up, the producer thread may wait forever in k_sem_take, leading to a deadlock situation. Signed-off-by: Fei Wang <[email protected]>
| 
 | 






One thread calls mpsc_pbuf_alloc to produce data, which invokes add_skip_item and steps into k_sem_take.
Another thread calls mpsc_pbuf_claim to consume data. In this condition, mpsc_pbuf_claim has only small remaining space and needs to call rd_idx_inc to reserve space, but there is still no data available.
The consumer should call k_sem_give to wake mpsc_pbuf_alloc again, so the producer can allocate space and continue producing data.
Without this wake-up, the producer thread may wait forever in k_sem_take, leading to a deadlock situation.